package com.xiefayang.m_groupTask01;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricIdentityLink;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.junit.Test;

/**
 * 组任务 : 
 * 
 *  	这里使用流程变量来指定组任务任务办理人, 对应task.bpmn中的CandidateUser -> "${userIds}"
 */
public class TaskTest {
	
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

	/**
	 * 部署流程定义(从classpath)
	 */
	@Test
	public void deploymentProcessDefinition(){
		
		Deployment deployment = processEngine.getRepositoryService() 
				.createDeployment() //创建一个部署对象
				.name("组任务") //添加部署的名称
				.addClasspathResource("diagrams/groupTask01.bpmn") 
				.addClasspathResource("diagrams/groupTask01.png")
				.deploy(); //完成部署
		
		System.out.println("部署ID: " + deployment.getId());
		System.out.println("部署名称: " + deployment.getName());
	}
	
	
	/**
	 * 启动流程实例
	 */
	@Test
	public void startProcessInstance() {
		
		String processDefinitionKey = "task";
		
		Map<String, Object> variables = new HashMap<String, Object>();
		
		//这里指定多个用户, 逗号分隔
		variables.put("userIds", "一一,二二,三三");
		ProcessInstance processInstance = this.processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey, variables);
		
		System.out.println("流程实例ID: " + processInstance.getId());
		System.out.println("流程定义ID: " + processInstance.getProcessDefinitionId());
	}
	
	
	/**
	 * 查询当前人的个人任务 
	 * 
	 * 		这里肯定是查不到的, 因为不是个人任务
	 */
	@Test
	public void findMyPerosonalTask() {
		
		String assignee = "大大";
		List<Task> taskList = this.processEngine.getTaskService()
				.createTaskQuery().taskAssignee(assignee).orderByTaskCreateTime().asc().list();
		
		for (Task task: taskList) {
			
			System.out.println("任务ID:"+task.getId());
			System.out.println("任务名称:"+task.getName());
			System.out.println("任务的创建时间:"+task.getCreateTime());
			System.out.println("任务的办理人:"+task.getAssignee());
			System.out.println("流程实例ID："+task.getProcessInstanceId());
			System.out.println("执行对象ID:"+task.getExecutionId());
			System.out.println("流程定义ID:"+task.getProcessDefinitionId());
			System.out.println("########################################################");
		}
	}
	
	/**
	 * 查询当前人的组任务
	 */
	 
	/**<p>功能描述：</p>
	 * <p>谢发扬</p>	
	 * @since JDK1.8
	 * <p>创建日期:2017年12月14日 下午4:30:15</p>
	 * <p>更新日期:[日期YYYY-MM-DD][更改人姓名][变更描述]</p>
	 */
	@Test
	public void findMyGroupTask() {
		
		//candidateUser 候选用户
		String candidateUser = "三三";
		List<Task> taskList = this.processEngine.getTaskService()
				.createTaskQuery().taskCandidateUser(candidateUser).list();
		
		for (Task task: taskList) {
			
			System.out.println("任务ID:"+task.getId());
			System.out.println("任务名称:"+task.getName());
			System.out.println("任务的创建时间:"+task.getCreateTime());
			System.out.println("任务的办理人:"+task.getAssignee());
			System.out.println("流程实例ID："+task.getProcessInstanceId());
			System.out.println("执行对象ID:"+task.getExecutionId());
			System.out.println("流程定义ID:"+task.getProcessDefinitionId());
			System.out.println("########################################################");
		}
	}
	
	
	/**
	 * 完成我的任务
	 */
	@Test
	public void completeMyPersonalTask() {
		
		//任务ID
		String taskId = "70005";
		this.processEngine.getTaskService().complete(taskId);
		System.out.println("完成任务: " + taskId);
	}
	
	
	/**
	 * 查询正在执行的任务的办理人表
	 */
	@Test
	public void findRunPersonTask() {
		
		String taskId = "70005";
		List<IdentityLink> list = this.processEngine.getTaskService().getIdentityLinksForTask(taskId);
		
		for (IdentityLink identityLink: list) {
			System.out.println(identityLink.getTaskId() + ", " + identityLink.getUserId() + ", " + identityLink.getType() + ", " + identityLink.getProcessInstanceId());
		}
	}
	
	
	/**
	 * 查询历史任务的办理人表
	 */
	@Test
	public void findHistoryPersonTask() {
		
		//流程实例ID
		String processInstanceId = "70001";
		List<HistoricIdentityLink> list = this.processEngine.getHistoryService().getHistoricIdentityLinksForProcessInstance(processInstanceId);
		
		for(HistoricIdentityLink identityLink:list){
			System.out.println(identityLink.getTaskId() + ", " + identityLink.getUserId() + ", " + identityLink.getType() + ", " + identityLink.getProcessInstanceId());
		}
	}
	
	
	/**
	 * 拾取任务: 将组任务分给个人任务, 指定任务的办理人
	 */
	@Test
	public void claim() {
		
		String taskId = "70005";
		
		//分配的个人(可以是组任务中的成员, 也可以不是)
		String userId = "大大";
		
		this.processEngine.getTaskService().claim(taskId, userId);
	}
	
	/**
	 * 个人任务回退给组任务(前提是之前一定是个组任务)
	 */
	@Test
	public void setAssignee() {
		
		String taskId = "70005";
		this.processEngine.getTaskService().setAssignee(taskId, null);
	}
	
	
	/**
	 * 向组任务中添加成员
	 */
	@Test
	public void addGroupUser() {
		
		String taskId = "55005";
		String userId = "AAA";
		this.processEngine.getTaskService().addCandidateUser(taskId, userId);
	}
	
	
	/**
	 * 向组任务中删除成员
	 */
	@Test
	public void deleteGroupUser() {
		
		String taskId = "55005";
		String userId = "AAA";
		this.processEngine.getTaskService().deleteCandidateUser(taskId, userId);
	}
	
}
